(function(w, $) {
    'use strict';
    var TYPE = {
        "byte" : {name : "byte",primitive:true,default:function () {
              return  Math.floor(Math.random()*(128-0+1))+0;
            }},
        "short" : {name : "short",primitive:true,default:function () {
                return  Math.floor(Math.random()*(128-0+1))+0;
            }},
        "int" : {name : "int",primitive:true,default:function () {
                return  Math.floor(Math.random()*(128-0+1))+0;
            }},
        "long" : {name : "long",primitive:true,default:function () {
                return  Math.floor(Math.random()*(128-0+1))+0;
            }},
        "float" : {name : "float",primitive:true,default:function () {
               return  Math.random()*(128-0)+0;
            }},
        "double" : {name : "double",primitive:true,default:function () {
                return  Math.random()*(128-0)+0;
            }},
        "date": {name : "date",primitive:true,default:function () {
               return new Date().format('yyyy-MM-dd');
            }},
        "time": {name : "time",primitive:true,default:function () {
                return new Date().format('HH:mm:ss');
            }},
        "timestamp": {name : "timestamp",primitive:true,default:function () {
                return new Date().format('yyyy-MM-dd HH:mm:ss');
            }},
        "string" : {name : "string",primitive:true,default:function () {
                return Math.random().toString(36).slice(-8);
            }},
        "struct" : {name : "struct",primitive:false,default : null},
        "array" : {name : "array",primitive:false,default : null},
    };
    var DEFAULT = {
        data : undefined,
        field : "field",//字段名
        parentId : 'pid',
        children : "children",
        id : "id",
        isTree : true,//是否为数据结构数据
    }

    w.API = function(options){
        this.options = options;
        this.init();
    }

    w.API.prototype.init = function(){
        this.options =   $.extend({},DEFAULT,this.options);
    };
    w.API.prototype.mock = function (data) {
        if(!data){ return null ;}
        var result;
        //根节点
        if(!TYPE[data.type]){ //未识别类型
            result = "未识别该类型";
        }else if(TYPE[data.type].primitive){ //基础类型
            result =  TYPE[data.type].default();
        }else if(data.type == TYPE.array.name && TYPE[data.genericType].default){//数组并且泛型为基本类型
            result = [];
            for(var i = 0 ; i < 2 ; i++){
                result[i] = TYPE[data.genericType].default();
            }
        }else if(data.type == TYPE.struct.name){//map
            result = {};
            for(var key in data[this.options.children]){
                var record = data[this.options.children][key];
                result[record[this.options.field]] = this.mock(record);
            }
        }else if(data.type == TYPE.array.name && TYPE.struct.name == data.genericType){ //数组并且泛型为map
            result = [];
            for(var i = 0 ; i < 2 ; i++){
                var struct = {};
                for(var key in data[this.options.children]){
                    var record = data[this.options.children][key];
                    struct[record[this.options.field]]  = this.mock(record);
                }
                result[i] = struct;
            }
        }
        return  result;
    };

    w.API.mock = function (options) {
        var api = new API(options);
        return api.mock(api.options.data);
    };
}(window, jQuery));